package client.gui.panels;

import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import controllers.ControllerIF;
import controllers.EvaluationReportController;
import entities.EvaluationReportStage;
import entities.Ticket;


public class EvaluationReportPanel extends JPanel implements PanelIF, ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * 
	 */
	private EvaluationReportController evaluationReportController;
	
	/**
	 * reference to the current visible ticket
	 */
	private Ticket ticket;

	
	private JPanel stageDurationPanel;
	private JLabel timeFrameLabel;
	
	private JPanel contentPanel;
	
	private JScrollPane changeDescScroll;
	private JPanel changeDescPanel;
	private JLabel changeDescLable;
	private	JTextArea changeDescTextArea;
	
	private JScrollPane resultScroll;
	private JPanel resultPanel;
	private JLabel resultLabel;
	private JTextArea resultTextArea;
	
	private JScrollPane constraintsScroll;
	private JPanel constraintsPanel;
	private JLabel constraintsLabel;
	private JTextArea constraintsTextArea;
	
	private JScrollPane risksScroll;
	private JPanel risksPanel;
	private JLabel risksLabel;
	private JTextArea risksTextArea;
	
	private JScrollPane durationScroll;
	private JPanel durationPanel;
	private JLabel durationLabel;
	private JTextArea durationTextArea;
	
	private JPanel buttonsPanel;
	private JLabel requiredFieldsLabel;	
	private JButton btnSend;
	private JButton btnClear;
	

	
	public EvaluationReportPanel(Ticket ticket) {
		this.ticket = ticket;
		initGUI();
	}

	/**
	 * Initialize the UI
	 */
	public void initGUI() {
		
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		add(Box.createVerticalStrut(10));
		
		{
			stageDurationPanel = new JPanel();
			add(stageDurationPanel);
			stageDurationPanel.setLayout(new BoxLayout(stageDurationPanel, BoxLayout.X_AXIS));
			
			stageDurationPanel.add(Box.createHorizontalStrut(20));
			
			timeFrameLabel = new JLabel("Due date for this stage: " + ticket.getEvaluationReportStage().getDurationDate());
			stageDurationPanel.add(timeFrameLabel);
			
			stageDurationPanel.add(Box.createHorizontalGlue());
		}
		
		add(Box.createVerticalStrut(10));
		
		contentPanel = new JPanel();
		add(contentPanel);
		contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
		{
			{
				changeDescPanel = new JPanel();
				contentPanel.add(changeDescPanel);
				changeDescPanel.setLayout(new BoxLayout(changeDescPanel, BoxLayout.X_AXIS));
				
				changeDescLable = new JLabel("* Necessary Change Description:");
				changeDescPanel.add(changeDescLable);
				changeDescLable.setForeground(Color.BLACK);
				
				changeDescPanel.add(Box.createHorizontalStrut(32));
				
				changeDescScroll = new JScrollPane();
				changeDescPanel.add(changeDescScroll);
				
				changeDescTextArea = new JTextArea();
				changeDescScroll.setViewportView(changeDescTextArea);
				changeDescTextArea.setRows(6);
				changeDescTextArea.setLineWrap(true);
			}
			
			contentPanel.add(Box.createVerticalStrut(10));
			
			{
				resultPanel = new JPanel();
				contentPanel.add(resultPanel);
				resultPanel.setLayout(new BoxLayout(resultPanel, BoxLayout.X_AXIS));
				
				resultLabel = new JLabel("* The obtain result:");
				resultPanel.add(resultLabel);
				resultLabel.setForeground(Color.BLACK);
				
				resultPanel.add(Box.createHorizontalStrut(98));
				
				resultScroll = new JScrollPane();
				resultPanel.add(resultScroll);
				
				resultTextArea = new JTextArea();
				resultTextArea.setLineWrap(true);
				resultTextArea.setRows(6);
				resultScroll.setViewportView(resultTextArea);
			}
			
				contentPanel.add(Box.createVerticalStrut(10));
			
			{
				constraintsPanel = new JPanel();
				contentPanel.add(constraintsPanel);
				constraintsPanel.setLayout(new BoxLayout(constraintsPanel, BoxLayout.X_AXIS));
				
				constraintsLabel = new JLabel("* Constraints:");
				constraintsPanel.add(constraintsLabel);
				constraintsLabel.setForeground(Color.BLACK);
				
				constraintsPanel.add(Box.createHorizontalStrut(123));
				
				constraintsScroll = new JScrollPane();
				constraintsPanel.add(constraintsScroll);
				
				constraintsTextArea = new JTextArea();
				constraintsTextArea.setLineWrap(true);
				constraintsTextArea.setRows(6);
				constraintsScroll.setViewportView(constraintsTextArea);
			}

			contentPanel.add(Box.createVerticalStrut(10));
			
			risksPanel = new JPanel();
			contentPanel.add(risksPanel);
			risksPanel.setLayout(new BoxLayout(risksPanel, BoxLayout.X_AXIS));
			
			risksLabel = new JLabel("* Risks:");
			risksPanel.add(risksLabel);
			
			risksPanel.add(Box.createHorizontalStrut(155));
			
			risksScroll = new JScrollPane();
			risksPanel.add(risksScroll);
			
			risksTextArea = new JTextArea();
			risksTextArea.setRows(6);
			risksScroll.setViewportView(risksTextArea);
			
			contentPanel.add(Box.createVerticalStrut(10));
			
			{
				durationPanel = new JPanel();
				contentPanel.add(durationPanel);
				durationPanel.setLayout(new BoxLayout(durationPanel, BoxLayout.X_AXIS));
				
				durationLabel = new JLabel("* Making Duration Assessment:");
				durationPanel.add(durationLabel);
				durationLabel.setForeground(Color.BLACK);
				
				durationPanel.add(Box.createHorizontalStrut(42));
				
				durationScroll = new JScrollPane();
				durationPanel.add(durationScroll);
				
				durationTextArea = new JTextArea();
				durationTextArea.setRows(6);
				durationTextArea.setLineWrap(true);
				durationScroll.setViewportView(durationTextArea);
			}

			contentPanel.add(Box.createVerticalStrut(10));
			
			{
				buttonsPanel = new JPanel();
				contentPanel.add(buttonsPanel);
				buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS));
				
				requiredFieldsLabel = new JLabel("* Required fields");
				buttonsPanel.add(requiredFieldsLabel);
				requiredFieldsLabel.setFont(new Font("Tahoma", Font.BOLD, 12));
				
				buttonsPanel.add(Box.createHorizontalGlue());
				
				btnClear = new JButton("Clear");
				buttonsPanel.add(btnClear);
				btnClear.addActionListener(this);
				
				buttonsPanel.add(Box.createHorizontalStrut(20));
				
				btnSend = new JButton("Send");
				buttonsPanel.add(btnSend);
				btnSend.addActionListener(this);
			}
		}
		
		setPanelEditable(true);
		
	}

	@Override	
	public void actionPerformed(ActionEvent e) {
		 if(e.getSource() == btnSend) {
			 /*****************************************************
			  ** Checking if all the required fields were filled **
			  ****************************************************/
			if(changeDescTextArea.getText().isEmpty() || constraintsTextArea.getText().isEmpty() || resultTextArea.getText().isEmpty() || durationTextArea.getText().isEmpty()) {
				if(changeDescTextArea.getText().isEmpty()) {					
					changeDescLable.setForeground(Color.RED);
				}
				else {					
					changeDescLable.setForeground(Color.BLACK);
				}
					
				if(constraintsTextArea.getText().isEmpty()) {					
					constraintsLabel.setForeground(Color.RED);
				}
				else {					
					constraintsLabel.setForeground(Color.BLACK);
				}
						
				if(resultTextArea.getText().isEmpty()) {					
					resultLabel.setForeground(Color.RED);
				}
				else {					
					resultLabel.setForeground(Color.BLACK);
				}
						
				if(durationTextArea.getText().isEmpty()) {					
					durationLabel.setForeground(Color.RED);
				}
				else {					
					durationLabel.setForeground(Color.BLACK);
				}
			}
			// in case all required fields are holding data - ticket is ready to be submitted
			else {
				evaluationReportController.action();
			}
		 }
		 else if (e.getSource() == btnClear) {
			changeDescTextArea.setText("");
			resultTextArea.setText("");
			constraintsTextArea.setText("");
			durationTextArea.setText("");		 
		}
	}

	@Override
	public void setController(ControllerIF cif) {
		evaluationReportController = (EvaluationReportController) cif;
	}

	/**
	 * Get the paragraph of reasons for change
	 * @return String
	 */
	public String getReasonTextArea() {
		return changeDescTextArea.getText();
	}

	/**
	 * Set all text areas and buttons editable / uneditable according to a given parameter
	 * @param bool boolean
	 */
	public void setPanelEditable(Boolean bool) {
		Color bgColor;
		
		if(bool == true) {
			bgColor = SystemColor.WHITE;
		}
		else
			bgColor = SystemColor.controlHighlight;
		
		changeDescTextArea.setEditable(bool);
		changeDescTextArea.setBackground(bgColor);
		resultTextArea.setEditable(bool);
		resultTextArea.setBackground(bgColor);
		risksTextArea.setEditable(bool);
		risksTextArea.setBackground(bgColor);
		constraintsTextArea.setEditable(bool);
		constraintsTextArea.setBackground(bgColor);
		durationTextArea.setEditable(bool);
		durationTextArea.setBackground(bgColor);
		btnSend.setEnabled(bool);
		btnClear.setEnabled(bool);
	}

	public EvaluationReportStage getEvaluationReportStage() {
		return new EvaluationReportStage(ticket.getTicket_id(), changeDescTextArea.getText(), resultTextArea.getText(), constraintsTextArea.getText(), risksTextArea.getText(), durationTextArea.getText());
	}
}
